//
//  WTHotMainViewController.m
//  OneDay
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTHotMainViewController.h"
#import "WTRequestManager.h"
#import "WTRecommendListModel.h"
#import "WTRecommendListCell.h"
#import "WTRecommendListTwoCell.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

@interface WTHotMainViewController ()<UITableViewDataSource,UITableViewDelegate>

// 创建tableView
@property (nonatomic, strong) UITableView *tableView;

// 创建一个数组,用于存放解析完数据的数组
@property (nonatomic, strong) NSMutableArray *listArray;
// 请求从第一个开始
@property (nonatomic, assign) NSInteger requestStart;

// 这个list是用于记录第几个界面的
@property (nonatomic, strong) NSString * list;

// 等待数据加载完成
@property (nonatomic, strong) MBProgressHUD * hud;
@end

@implementation WTHotMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _requestStart = 0;

    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, kScreenHeight - 64 - 49 - 50 * kRelativelength_Y)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    // 注册
    [_tableView registerClass:[WTRecommendListCell class] forCellReuseIdentifier:@"WTRecommendListCell"];
    [_tableView registerClass:[WTRecommendListTwoCell class] forCellReuseIdentifier:@"WTRecommendListTwoCell"];
    
    
    // 初始化数组
    _listArray = [NSMutableArray array];
    
    
#pragma mark -- 明杰刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 0;
        [self requestData:_list];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requestStart++;
        [self requestData:_list];
    }];
    
    [self setupHud];
    // Do any additional setup after loading the view.
}

#pragma mark -- 等待数据加载完成
- (void)setupHud
{
    _hud = [[MBProgressHUD alloc]initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.labelText = @"正在努力加载...";
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}

#pragma mark -- 数据解析的方法
- (void)requestData:(NSString *)list
{
     _list = list;
 
    // 请求网址通过list改变
   NSString *url = [NSString stringWithFormat:@"http://api.wap.miercn.com/api/2.0.3/newlist.php?list=%@&page=%ld&plat=android&proct=mierapp&versioncode=20150807&apiCode=4",list,(long)_requestStart];
   [WTRequestManager requestWithUrlString:url parDic:nil method:GET success:^(NSData *data) {
       
       if (_requestStart == 1) {
           [_listArray removeAllObjects];
       }

       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       NSArray *newsArray = dic[@"newsList"];
       for (NSDictionary *oneDic in newsArray) {
           WTRecommendListModel *model = [[WTRecommendListModel alloc]init];
           [model setValuesForKeysWithDictionary:oneDic];
           [_listArray addObject:model];
       }
       
       
       [_tableView reloadData];
       [_tableView.mj_footer endRefreshing];
       [_tableView.mj_header endRefreshing];
       [_hud hide:YES];
   } error:^(NSError *error) {
       
   }];
    
}
#pragma mark -- dataSource方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WTRecommendListModel *model = _listArray[indexPath.row];
    // 判断,如果第一张图片存在,并且第三张图片不存在 是1种情况
    // 其他是另一种情况
    if (![model.picOne isEqualToString:@""] && [model.picThr isEqualToString:@""]) {
        WTRecommendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WTRecommendListCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else {
        WTRecommendListTwoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WTRecommendListTwoCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    
}

#pragma mark --  返回每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTRecommendListModel *model = _listArray[indexPath.row];
    if (![model.picOne isEqualToString:@""] && [model.picThr isEqualToString:@""]) {
        return [WTRecommendListCell heightForRow:model];
    }else {
        
        return [WTRecommendListTwoCell heightForRow:model];
    }
}

// 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTRecommendListModel * model = _listArray[indexPath.row];
    self.block(model.ID,model.commentNum);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
